package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.ReportRhlqSssylBaDetail;
import com.ruoyi.report_lq.domain.vo.ReportRhlqSssylBaDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportRhlqYcjldnfxjlbBaDetailBo;
import com.ruoyi.report_lq.domain.vo.ReportRhlqYcjldnfxjlbBaDetailVo;
import com.ruoyi.report_lq.domain.ReportRhlqYcjldnfxjlbBaDetail;
import com.ruoyi.report_lq.mapper.ReportRhlqYcjldnfxjlbBaDetailMapper;
import com.ruoyi.report_lq.service.IReportRhlqYcjldnfxjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 乳化沥青与粗集料的黏附性记录Service业务层处理
 *
 * @author zyb
 * @date 2024-07-02
 */
@RequiredArgsConstructor
@Service
public class ReportRhlqYcjldnfxjlbBaDetailServiceImpl implements IReportRhlqYcjldnfxjlbBaDetailService {

    private final ReportRhlqYcjldnfxjlbBaDetailMapper baseMapper;

    /**
     * 查询乳化沥青与粗集料的黏附性记录
     */
    @Override
    public ReportRhlqYcjldnfxjlbBaDetailVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     *
     * @param infoId
     * @return 黏附性集合
     * @method 根据info-id 查询与粗集料黏附性子表的list集合数据
     */
    public List<ReportRhlqYcjldnfxjlbBaDetailVo> queryByInfoId (Long infoId) {
        QueryWrapper<ReportRhlqYcjldnfxjlbBaDetail> query = new QueryWrapper<>();
        query.eq("common_id", infoId);
        return baseMapper.selectVoList(query);
    }

    /**
     * 查询乳化沥青与粗集料的黏附性记录列表
     */
    @Override
    public TableDataInfo<ReportRhlqYcjldnfxjlbBaDetailVo> queryPageList(ReportRhlqYcjldnfxjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportRhlqYcjldnfxjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportRhlqYcjldnfxjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询乳化沥青与粗集料的黏附性记录列表
     */
    @Override
    public List<ReportRhlqYcjldnfxjlbBaDetailVo> queryList(ReportRhlqYcjldnfxjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportRhlqYcjldnfxjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportRhlqYcjldnfxjlbBaDetail> buildQueryWrapper(ReportRhlqYcjldnfxjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportRhlqYcjldnfxjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommonId() != null, ReportRhlqYcjldnfxjlbBaDetail::getCommonId, bo.getCommonId());
        lqw.eq(StringUtils.isNotBlank(bo.getBianh()), ReportRhlqYcjldnfxjlbBaDetail::getBianh, bo.getBianh());
        lqw.eq(bo.getJillj() != null, ReportRhlqYcjldnfxjlbBaDetail::getJillj, bo.getJillj());
        lqw.eq(bo.getJiljssj() != null, ReportRhlqYcjldnfxjlbBaDetail::getJiljssj, bo.getJiljssj());
        lqw.eq(bo.getRuyzjpsj() != null, ReportRhlqYcjldnfxjlbBaDetail::getRuyzjpsj, bo.getRuyzjpsj());
        lqw.eq(bo.getJilzswxfzsj() != null, ReportRhlqYcjldnfxjlbBaDetail::getJilzswxfzsj, bo.getJilzswxfzsj());
        lqw.eq(bo.getJinzsj() != null, ReportRhlqYcjldnfxjlbBaDetail::getJinzsj, bo.getJinzsj());
        lqw.eq(bo.getJinzsw() != null, ReportRhlqYcjldnfxjlbBaDetail::getJinzsw, bo.getJinzsw());
        lqw.eq(bo.getCujlklslqmdgfmj() != null, ReportRhlqYcjldnfxjlbBaDetail::getCujlklslqmdgfmj, bo.getCujlklslqmdgfmj());
        lqw.eq(bo.getZonghpd() != null, ReportRhlqYcjldnfxjlbBaDetail::getZonghpd, bo.getZonghpd());
        return lqw;
    }

    /**
     * 新增乳化沥青与粗集料的黏附性记录
     */
    @Override
    public Boolean insertByBo(ReportRhlqYcjldnfxjlbBaDetailBo bo) {
        ReportRhlqYcjldnfxjlbBaDetail add = BeanUtil.toBean(bo, ReportRhlqYcjldnfxjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改乳化沥青与粗集料的黏附性记录
     */
    @Override
    public Boolean updateByBo(ReportRhlqYcjldnfxjlbBaDetailBo bo) {
        ReportRhlqYcjldnfxjlbBaDetail update = BeanUtil.toBean(bo, ReportRhlqYcjldnfxjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 乳化沥青筛上剩余量、微粒离子电荷试验检测记录表  -- 子表操作
     */
    public Boolean insertOrUpdateByNfxBo(List<ReportRhlqYcjldnfxjlbBaDetailVo> detailVos, Long pId) {
        detailVos.forEach(detailVo -> {
            ReportRhlqYcjldnfxjlbBaDetail detail = new ReportRhlqYcjldnfxjlbBaDetail();
            detail.setExcelId(detailVo.getExcelId());
            detail.setCommonId(pId);
            detail.setBianh(detailVo.getBianh());
            detail.setJillj(detailVo.getJillj());
            detail.setJiljssj(detailVo.getJiljssj());
            detail.setRuyzjpsj(detailVo.getRuyzjpsj());
            detail.setJilzswxfzsj(detailVo.getJilzswxfzsj());
            detail.setJinzsj(detailVo.getJinzsj());
            detail.setJinzsw(detailVo.getJinzsw());
            detail.setCujlklslqmdgfmj(detailVo.getCujlklslqmdgfmj());
            detail.setZonghpd(detailVo.getZonghpd());
            baseMapper.insertOrUpdate(detail);
        });
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportRhlqYcjldnfxjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除乳化沥青与粗集料的黏附性记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
